package cn.hxzy.zyicon.controller;

import cn.hxzy.zyicon.entity.Icon;
import cn.hxzy.zyicon.entity.Image;
import cn.hxzy.zyicon.entity.Resource;
import cn.hxzy.zyicon.entity.User;
import cn.hxzy.zyicon.entity.po.ImagePO;
import cn.hxzy.zyicon.entity.po.ResourcePO;
import cn.hxzy.zyicon.entity.vo.PageBean;
import cn.hxzy.zyicon.entity.vo.ReturnBean;
import cn.hxzy.zyicon.service.ImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("image")
public class ImageController {
    @Autowired
    private ImageService imageService;

    @PostMapping("list")
    public ReturnBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageBean<Image> all = imageService.findAll(new PageBean<Image>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    @PostMapping("searchName")
    public ReturnBean searchName(String searchName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageBean<Image> all = imageService.findImageByNameLike(searchName, new PageBean<Image>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    @PostMapping("deleteId")
    public ReturnBean deleteId(Integer id) {
        imageService.delete(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("add")
    public ReturnBean add(Image image, MultipartFile file) throws IOException {
        if (file!=null&&!file.isEmpty()){
            String uuid=UUID.randomUUID().toString().replaceAll("-","");
            String fileName = file.getOriginalFilename();
            String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".")) : null;
            File file1=new File("C:/resources/image/"+uuid+type);
            file.transferTo(file1);
            image.setUrl(uuid+type);
        }
        imageService.add(image);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Image byId = imageService.findById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,byId);
    }
    @PostMapping("update")
    public ReturnBean update( MultipartFile file, ImagePO imagePo) throws IOException {
        if (!file.isEmpty()) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String fileName = file.getOriginalFilename();
            String path = "C:/resources/image/";
            String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".")) : null;
            File file1 = new File(path + uuid + type);
            file.transferTo(file1);
            Image image = new Image();
            BeanUtils.copyProperties(imagePo, image);
            image.setUrl("/image/" + uuid+type);
            imageService.update(image);
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.OBJECT_NOT_EXIST);
    }

}
